/*
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lntea.lnblog.config;

import java.io.*;
import java.net.URL;
import java.util.Properties;

/**
 * 配置信息类
 *
 * @author lichao
 * @date 2016/6/28 13:32
 */
public class Configuration {

    private static final Properties prop = new Properties();

    /**
     * 加载配置文件，初始化信息
     */
    static {
        //加载properties配置文件
        URL rootUrl = Configuration.class.getClassLoader().getResource("");
        if (rootUrl != null) {
            File rootFile = new File(rootUrl.getFile());
            if (rootFile.isDirectory()) {
                File[] files = rootFile.listFiles();
                for (File f : files) {
                    if (f.getName().endsWith(".properties") && !f.getName().startsWith("log4j")) {
                        try {
                            FileInputStream is = new FileInputStream(f);
                            prop.load(is);
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    /**
     * 根据key获取配置的value
     * @param key
     * @return
     */
    public static String getProperty(String key){
        return prop.getProperty(key);
    }

    /**
     * 获取classpath下的路径
     * @param packageName 包名
     * @return
     */
    public static String getClassPathFileName(String packageName){
        packageName = packageName.replaceAll("\\.","/");
        return Configuration.class.getClassLoader().getResource(packageName).getPath();
    }
}
